package com.ubao.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.ubao.global.TimeManager;
import com.ubao.network.HttpHandler;
import com.ubao.network.HttpListener;
import com.ubao.network.RequestInterface;
import com.ubao.network.ResponseInterface;
import com.ubao.network.manager.NetWorkNtfyManager;


public abstract class RequestBaseActivity extends BaseActivity implements
		HttpListener {

	protected ProgressBar mProgressBar;

	protected boolean mRequestSuccess = true;

	protected AlertDialog mAlertDialog;

	protected HttpHandler mHttpHandler = new HttpHandler(this);

	public boolean requestSuccess = true;
	
	private NetWorkNtfyManager netWorkNtfyManager;

	protected void send() {
		startProgress();
	}

	protected abstract void httpCompleted(ResponseInterface resp);

	@Override
	public void completed(ResponseInterface resp) {
		// TODO Auto-generated method stub
		try {
			httpCompleted(resp);
		} catch (Exception e) {
			e.printStackTrace();
			failProgress("数据错误");
		}
	}

	@Override
	public void exception(Exception e, HttpHandler handler) {
		failProgress(e.getMessage());
	}

	@Override
	public boolean acceptResponse(RequestInterface request) {
		// TODO Auto-generated method stub
		return true;
	}

	protected void startProgress() {
		startProgress(0);
	}
	
	protected void startProgress(int no) {
//		if(getTitleBar()!=null){
//			startNotificationProgress();
//		}else{
			startDialogProgress(no);
//		}
		
	}
	
	private void startNotificationProgress(){
		startTitleProgressHandler.sendEmptyMessage(0);
	}
	
	private Handler startTitleProgressHandler = new Handler() {
//		public void handleMessage(Message msg) {
//			GTitleBar titleBar = getTitleBar();
//			titleBar.startProgressbar();
//		}
	};
	
	

	protected void startDialogProgress(int no) {
		if (mProgressBar == null
				|| mProgressBar.getVisibility() != View.VISIBLE) {
			startProgressHandler.sendEmptyMessage(no);
		} else {
			mProgressBar.setTag(String.valueOf(no));
		}
	}

	private Handler startProgressHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			final Activity activity = RequestBaseActivity.this;

			if (mProgressBar == null && !isFinishing()) {
				mProgressBar = new ProgressBar(activity);
				mProgressBar.setBackgroundColor(Color.TRANSPARENT);
				mProgressBar.setIndeterminate(true);
				final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
				layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
				layoutParams.format = PixelFormat.RGBA_8888;
				layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
				layoutParams.width = 40;
				layoutParams.height = 40;
				activity.getWindowManager().addView(mProgressBar, layoutParams);
			}

			if (mProgressBar != null) {
				mProgressBar.setVisibility(View.VISIBLE);
				mProgressBar.setTag(String.valueOf(msg.what));
			}

			// TODO Auto-generated method stub
			super.handleMessage(msg);
		}
	};
	
	public boolean closeProgress() {
		
		//先关闭titleBar上的progress
//		if(getTitleBar()!=null){
//			
//			closeTitleProgressHandler.sendEmptyMessage(0);
//			return true;
//		}
		
		
		//再关闭dialog
		return closeProgress(Integer.MAX_VALUE);
	}
	
	public boolean closeProgressAndRightButton(){
		//先关闭titleBar上的progress
//		if(getTitleBar()!=null){
//			
//			closeTitleProgressHandler.sendEmptyMessage(1);
//			return true;
//		}
		
		
		//再关闭dialog
		return closeProgress(Integer.MAX_VALUE);
	}

	private Handler closeTitleProgressHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
//			GTitleBar titleBar = getTitleBar();
//			if(titleBar!=null){
//				if(msg.what==0){
//					titleBar.stopProgressbar();
//				}else if(msg.what==1){
//					titleBar.stopProgressbarAndHideRightButtons();
//				}
//			}
		}
	};

	public boolean closeProgress(int no) {
		if (mProgressBar == null) {
			return false;
		}

		int recordNo = Integer.MAX_VALUE;
		final String tag = (String) mProgressBar.getTag();
		if (tag != null) {
			recordNo = Integer.parseInt(tag);
		}

		if (no < recordNo) {
			return false;
		}

		if (mProgressBar.getVisibility() == View.VISIBLE) {
			closeProgressHandler.sendEmptyMessage(0);
			return true;
		}

		return false;
	}
	

	protected final void closeFailProgress() {
		if (mAlertDialog != null && mAlertDialog.isShowing())
			mAlertDialog.cancel();
	}

	private Handler closeProgressHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (mProgressBar != null)
				mProgressBar.setVisibility(View.INVISIBLE);

			// TODO Auto-generated method stub
			super.handleMessage(msg);
		}
	};

	protected final void failProgress(String message) {
		//if (closeProgress()) {
			message = (message == null | message == "") ? "数据为空" : message;

			Message msg = new Message();
			msg.obj = message;
			failHandler.sendMessage(msg);
		//}
	}

	private final Handler failHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (isFinishing()) {
				return;
			}
			NetWorkNtfyManager.getInstance(RequestBaseActivity.this).sendNotify((String)msg.obj);
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		if(mHttpHandler==null){
			mHttpHandler = new HttpHandler(this, 1000);
		}
		netWorkNtfyManager = NetWorkNtfyManager.getInstance(this);
		netWorkNtfyManager.setShowFlag(true);
	}

	@Override
	public void onPause() {
		closeProgress();
		if (mHttpHandler != null) {
			mHttpHandler.stop();
			mHttpHandler = null;
		}
		if(this.netWorkNtfyManager!=null){
			netWorkNtfyManager.setShowFlag(false);
		}
		TimeManager.clean();
		super.onPause();
	}
	
	/**
	 * 获取子类中需要刷新的titleBar
	 * 返回null,会用旧的方式刷新 
	 * @return
	 */
//	protected GTitleBar getTitleBar(){
//		return null;
//	}
	
	
	
	
}
